﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Reflection;

namespace com.nm.library.Excels.Mef
{
    public class MefConfig
    {
        private static CompositionContainer _container;

        /// <summary>
        ///
        /// </summary>
        public static void Init()
        {
            //1.Mef接管
            // var catalog = new AssemblyCatalog(typeof(ExcelImport).Assembly);
            // var catalog = new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly());
            //var catalog = new DirectoryCatalog(AppDomain.CurrentDomain.BaseDirectory);
            //var catalog = new DirectoryCatalog(AppDomain.CurrentDomain.SetupInformation.PrivateBinPath);
            // _container = new CompositionContainer(catalog);
            //_container.ComposeParts();

            //var catalog = new DirectoryCatalog(AppDomain.CurrentDomain.SetupInformation.PrivateBinPath);
            //_container = new CompositionContainer(catalog);
            //_container.ComposeParts();

            //从目录加载dll
            var catalog = new AggregateCatalog();
            //从exe中获取Parts
            //catalog.Catalogs.Add(new DirectoryCatalog(path, "MRS.UI.*.exe"));
            //从dll中获取Parts ,指定类库集的原因(解决无法加载一个或多个请求的类型错误)
            catalog.Catalogs.Add(new DirectoryCatalog(AppDomain.CurrentDomain.SetupInformation.PrivateBinPath, "TerminalAPI.dll"));
            catalog.Catalogs.Add(new DirectoryCatalog(AppDomain.CurrentDomain.SetupInformation.PrivateBinPath, "VerticaAPI.dll"));
            catalog.Catalogs.Add(new DirectoryCatalog(AppDomain.CurrentDomain.SetupInformation.PrivateBinPath, "PlanAPI.dll"));
            catalog.Catalogs.Add(new DirectoryCatalog(AppDomain.CurrentDomain.SetupInformation.PrivateBinPath, "DistributeAPI.dll"));
            catalog.Catalogs.Add(new DirectoryCatalog(AppDomain.CurrentDomain.SetupInformation.PrivateBinPath, "CsrdAPI.dll"));
            catalog.Catalogs.Add(new DirectoryCatalog(AppDomain.CurrentDomain.SetupInformation.PrivateBinPath, "CommodityTransferAPI.dll"));
            catalog.Catalogs.Add(new DirectoryCatalog(AppDomain.CurrentDomain.SetupInformation.PrivateBinPath, "OrderAPI.dll"));
            catalog.Catalogs.Add(new DirectoryCatalog(AppDomain.CurrentDomain.SetupInformation.PrivateBinPath, "InventoryToPredictAPI.dll"));
            //?同程序集中获取Parts
            catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
            //从各个dll获取内容
            _container = new CompositionContainer(catalog);
            _container.ComposeParts();
        }

        /// <summary>
        /// 获取解析类型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T TryResolve<T>()
        {
            return _container.GetExportedValueOrDefault<T>();
        }

        /// <summary>
        /// 获取解析类型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static IEnumerable<T> ResolveMany<T>()
        {
            return _container.GetExportedValues<T>();
        }

        public static T Resolve<T>()
        {
            return _container.GetExportedValue<T>();
        }
    }
}